package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.region.AdminRegionChildrenChildrenVO;
import com.cskaoyan.bean.vo.region.AdminRegionChildrenVO;
import com.cskaoyan.bean.vo.region.AdminRegionVO;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.cskaoyan.service.admin.AdminRegionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :rain
 * @description 根据pid判别省，市，县
 * @since 2022/06/26 16:24
 */
@Service
public class AdminRegionServiceImpl implements AdminRegionService {

    @Autowired
    MarketRegionMapper regionMapper;

    // 获取行政区域信息
    @Override
    public CommonData<AdminRegionVO> qureyRegionInfo() {
       /* MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        //拼接省份的Pid为0
        criteria.andPidEqualTo(0);
        //获取到0到31的省份信息
        List<MarketRegion> marketRegions = regionMapper.selectByExample(marketRegionExample);

        //返回省份的对象list
        List<AdminRegionVO> princeList = new ArrayList<>();
        //遍历region的前31个信息
        for (int i = 0; i < marketRegions.size(); i++) {
            //每一个省份都要生成市级list
            List<AdminRegionChildrenVO> childrenVOList = new ArrayList<>();
            //获取每个省份的id
            Integer regionId = marketRegions.get(i).getId();
            //根据每个省份的id获取当前省份的城市
            List<MarketRegion> cityList = regionMapper.selectCityById(regionId);
            //遍历城市，根据城市的id查询对应的县区
            for (int j = 0; j <cityList.size(); j++) {
                //获取当前城市的id
                Integer cityId = cityList.get(j).getId();
                //根据城市的id获取到对应的县区
                List<AdminRegionChildrenChildrenVO> zone = regionMapper.selectZoneByCityPid(cityId);
                //生成市区的对象
                AdminRegionChildrenVO adminRegionChildrenVO = new AdminRegionChildrenVO();
                //市区封装完成
                adminRegionChildrenVO.setChildren(zone);
                adminRegionChildrenVO.setCode(cityList.get(j).getCode());
                adminRegionChildrenVO.setId(cityList.get(j).getId());
                adminRegionChildrenVO.setName(cityList.get(j).getName());
                adminRegionChildrenVO.setType(Integer.valueOf(cityList.get(j).getType()));
                //将当前的市区对象封装到市区list中
                childrenVOList.add(adminRegionChildrenVO);
            }
            //生成省级区域的对象,封装市区
            AdminRegionVO adminRegionVO = new AdminRegionVO();
            adminRegionVO.setChildren(childrenVOList);
            adminRegionVO.setCode(marketRegions.get(i).getCode());
            adminRegionVO.setId(marketRegions.get(i).getId());
            adminRegionVO.setName(marketRegions.get(i).getName());
            adminRegionVO.setType(marketRegions.get(i).getType());
            //封装省级的list
            princeList.add(adminRegionVO);
        }
        CommonData<AdminRegionVO> adminRegionVOCommonData = new CommonData<>();
        adminRegionVOCommonData.setList(princeList);
        adminRegionVOCommonData.setPage(1);
        adminRegionVOCommonData.setLimit(31);
        adminRegionVOCommonData.setTotal(31);
        adminRegionVOCommonData.setPages(1);
        return adminRegionVOCommonData;*/


        //第二种方式
        //获取省
        List<AdminRegionVO> province = regionMapper.selectAllPrinceInfo(0);

        //遍历省
        for (int i = 0; i < province.size(); i++) {
            //获取省的id
            Integer provinceId = province.get(i).getId();
            //根据省的id获取到每个市的信息
            List<AdminRegionChildrenVO> city = regionMapper.selectCityById(provinceId);
            System.out.println(city);
            //将每个省份的城市放入对应的省份
            province.get(i).setChildren(city);
            for (int j = 0; j < city.size(); j++) {
                //获取每个city的id
                Integer cityId = city.get(j).getId();
                //获取每个县区的信息
                List<AdminRegionChildrenChildrenVO> zone = regionMapper.selectZoneByCityPid(cityId);
                System.out.println(zone);
                //将每个县区放入对应的市下
                city.get(j).setChildren(zone);
            }

        }
        PageInfo<AdminRegionVO> pageInfo = new PageInfo<>(province);
        return CommonData.data(pageInfo);

      /*第三种方式
       //获取所有的省
        List<AdminRegionVO> marketRegions = regionMapper.selectAllPrinceInfo(0);
        //获取所有的市
        List<AdminRegionChildrenVO> regionCity = regionMapper.selectAllCity(2);
        //获取所有的县
        List<AdminRegionChildrenChildrenVO> regionZone = regionMapper.selectAllZone(3);

        for (int i = 0; i < marketRegions.size(); i++) {
            Integer proinceId = marketRegions.get(i).getId();
            String string1 = proinceId.toString();
            List<AdminRegionChildrenVO> city = regionCity.stream().filter(AdminRegionChildrenVO -> AdminRegionChildrenVO.getPid().equals(string1)).collect(Collectors.toList());
          marketRegions.get(i).setChildren(city);
            for (int j = 0; j < regionCity.size(); j++) {
                Integer cityId = regionCity.get(j).getId();
                String string = cityId.toString();
                List<AdminRegionChildrenChildrenVO> zone = regionZone.stream().filter(AdminRegionChildrenChildrenVO -> AdminRegionChildrenChildrenVO.getPid().equals(string)).collect(Collectors.toList());
                regionCity.get(j).setChildren(zone);
            }
        }
        PageInfo<AdminRegionVO> pageInfo = new PageInfo<>(marketRegions);
        return CommonData.data(pageInfo);*/


    }
}
